<?php
Class Aggregator_Admin{
	
	public $site_url = null;
	
	public $rss_url = null;
	
	
	public function __construct(){
		add_action('admin_menu',array($this,'add_menu'));
		
		//add_action('admin_menu',array($this,'god_speak'));
		
		add_action('admin_enqueue_scripts',array($this,'add_js_and_css_admin'));
		
		add_action('wp_enqueue_scripts',array($this,'add_js_and_css_front'));
		
		add_action('wp_ajax_aggregator',array($this,'phare_rss_url'));
		
		add_action('init',array($this,'world_view_post_type'));
		add_action('init',array($this,'best_of_blog_post_type'));
		add_action('init',array($this,'video_post_type'));
		add_action('init',array($this,'humor_me_post_type'));
		add_action('init',array($this,'contest_post_type'));
		
		add_action('wp_ajax_post_it',array($this,'insert_data'));
		
		add_action('wp_head',array($this, 'ppbv_page_viewed')); // attach ppbv_page_viewed to the wp_head hook
		
		add_action('after_setup_theme',array($this,'create_dababase'));
	}
	public function create_dababase(){
	
		global $wpdb;
		$wpdb->query("CREATE TABLE IF NOT EXISTS `".$wpdb->prefix."popular_by_views` (
		  `id` bigint(50) NOT NULL AUTO_INCREMENT,
		  `post_id` varchar(255) NOT NULL,
		  `views` bigint(50) NOT NULL,
		  PRIMARY KEY (`id`),
		  UNIQUE KEY `id` (`id`)
		) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=37") ;
	}
	
	public function ppbv_page_viewed(){
		if(is_single() && !is_page()){ // only run on posts and not pages
			global $wpdb, $post;
			$ppbv_tablename = $wpdb->prefix.'popular_by_views';
			$wpdb->flush(); // clense the DB interface
			$data = $wpdb->get_row("SELECT * FROM {$ppbv_tablename} WHERE post_id='{$post->ID}'", ARRAY_A); // get the data row that has the matching post ID
			if(!is_null($data)){ // if we have a matching data row
				$new_views = $data['views'] + 1; // increase the views by 1
				$wpdb->query("UPDATE {$ppbv_tablename} SET views='{$new_views}' WHERE post_id='{$post->ID}';"); // update the data row with the new views
				$wpdb->flush(); // clense the DB interface
			}
			else { // if we don't have a matching data row (nobody's viewed the post yet)
				$wpdb->query("INSERT INTO {$ppbv_tablename} (post_id, views) VALUES ('{$post->ID}','1');"); // add a new data row into the DB with the post ID and 1 view
				$wpdb->flush(); // clense the DB interface
			}
		}
	}

	
	public function add_js_and_css_admin(){
		 wp_enqueue_script('jquery-ui-tabs');
		 
		 //jquery form
		
		//my custom js	
		 wp_register_script('mycustom',get_bloginfo('template_directory').'/include/js/mycustom.js');
		 wp_enqueue_script('mycustom');
		 
		 //add css
		 wp_register_style( 'customcss',get_bloginfo('template_directory').'/include/css/admin.css');
		 wp_enqueue_style('customcss');
	}
	
	public function add_js_and_css_front(){
		//anything slider
		wp_enqueue_script('jquery');
		wp_enqueue_script('jquery-ui-core');
		wp_enqueue_script('jquery-ui-widget');
		wp_enqueue_script('jquery-ui-tabs');
		
		wp_register_script('jquery_form',get_bloginfo('template_directory').'/include/js/jQuery_form.js');
		wp_enqueue_script('jquery_form');
		 
		wp_register_script('jquery.anythingslider.min',get_bloginfo('template_directory').'/include/js/jquery.anythingslider.min.js');
		wp_enqueue_script('jquery.anythingslider.min');
		//anything slider
		wp_register_style( 'anythingslider',get_bloginfo('template_directory').'/include/css/anythingslider.css');
		wp_enqueue_style('anythingslider');
		//jquery ui
		// wp_register_style( 'demo',get_bloginfo('template_directory').'/include/css/demo.css');
		// wp_enqueue_style('demo');
		
		//my custom js	
		 wp_register_script('mycustom_front',get_bloginfo('template_directory').'/include/js/mycustom_front.js');
		 wp_enqueue_script('mycustom_front');
	 
	}
	
	public function add_menu(){
		add_menu_page( 'Hope Unlimited','Hope Unlimited','add_users','Aggregator',array($this,'aggregator') );
	}
	
	public function god_speak(){
		//add_menu_page( 'God speak','God speak','add_users','God speak',array($this,'god_speak_tmp') );
	}
	
	public function god_speak_tmp(){
		//require_once('god_speak_tmp.php');
		
	}
	
	public function aggregator(){
		require_once('aggregator_tmp.php');
		
	}
	
	public function get_url(){
		
	}
	
	public function phare_rss_url(){
		
		include_once(ABSPATH . WPINC . '/feed.php');
		
		if(isset($_POST['website_url']))
		
			$this->site_url = $_POST['website_url'];	
		
			$this->rss_url = $_POST['website_url'];
			
		// Get a SimplePie feed object from the specified feed source.
		if($this->rss_url!=null) : 
		$rss = fetch_feed($this->rss_url);
		if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly 
			// Figure out how many total items there are, but limit it to 5. 
			$maxitems = $rss->get_item_quantity(5); 

			// Build an array of all the items, starting with element 0 (first element).
			$rss_items = $rss->get_items(0, $maxitems); 
		endif;
		ob_start();
		?>
		
		<ul class="World View-result">
			<?php if ($maxitems == 0) echo '<li>No items.</li>';
			else
			foreach ( $rss_items as $item ) : ?> 
			<?php
			echo $item->get_item_tags('','img')
			?>
			<li class="blog-item">
				<a class="blog-title" href='<?php echo esc_url( $item->get_permalink() ); ?>'
				title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>'>
				<?php echo esc_html( $item->get_title() ); ?></a>
				<div class="blog-content" style="display:none">				
					<div class="short-info"><?php echo $item->get_content() ; ?></div>
					<div class="action">						
						<a class="post-it" href="javascript:void(0);">Post it</a>	
					</div>
				</div>
			</li>
			<?php endforeach; ?>
			<script type="text/javascript">
				jQuery(function(){
					jQuery('.blog-item').hover(function(){
					jQuery(this).children('.blog-content').show();
					},function(){jQuery(this).children('.blog-content').hide();})
				});
				jQuery('.post-it').click(function(){
					
					jQuery('#list_post_get').hide();
					var title = jQuery(this).parent().parent().parent().children('.blog-title').html();					
					var content = jQuery(this).parent().parent().children('.short-info').html();
					jQuery('#aggregator_result #post_aggregator_title').val(jQuery.trim(title));
					jQuery('#aggregator_result #post_aggregator_content').val(jQuery.trim(content)) ;
					jQuery('#result').show();
					
				});
				jQuery('.post-it').click(function(){
					// var title = jQuery(this).parent().parent().parent().children('.blog-title').html();					
					// var content = jQuery(this).parent().parent().children('.short-info').html();
					// var data = {action : "post_it", post_title : title, post_content : content};
					// jQuery.ajax({
						// type : "POST",
						// url : ajaxurl,
						// data : data ,
						// success : function(data){
							// //alert('Success');
						// }
					// });
				});
			</script>
		</ul>
		
		<?php
		$result=ob_get_contents();
		else : echo "Correct the rss url plz";
		endif;
		ob_end_flush();
		die();
		return $result;
		
	}	
	
	public function insert_data(){
		$arg=array(
				 'post_title' => trim($_POST['post_title']),
				 'post_content' => trim($_POST['post_content']),
				 'post_status' => 'publish',
				 'post_type' => 'world_view',
				 'post_author' => 1,
			  );
		wp_insert_post( $arg );
	}
	
	
	public function world_view_post_type(){
		register_post_type('world_view', array(
			'labels' => array(
					'name' => _x('World View Items', 'post type general name'),
					'singular_name' => _x('World View Item', 'post type singular name'),
					'add_new' => _x('Add New', 'World View'),
					'add_new_item' => __('Add New World View Item'),
					'edit_item' => __('Edit World View Item'),
					'new_item' => __('New World View Item'),
					'view_item' => __('View World View Item'),
					'search_items' => __('Search World View Items'),
					'not_found' =>  __('No World View item found'),
					'not_found_in_trash' => __('No World View items found in Trash'),
					'parent_item_colon' => '',
					'menu_name' => __('World View Items'),
			),
			'singular_label' => __('World View'),
			'public' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'show_ui' => true,
			'show_in_menu' => true,
			//'menu_position' => 20,
			'capability_type' => 'post',
			'hierarchical' => false,
			'supports'  =>  array(
					  'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail',
					  'custom-fields'
					),
			'taxonomies' => array('category'), // this is IMPORTANT
			'has_archive' => true,
			'rewrite' =>  true,
			'query_var' => true,
			'can_export' => true,
			'show_in_nav_menus' => false,
		));
	}
	
	public function best_of_blog_post_type(){
		register_post_type('best_of_blog', array(
			'labels' => array(
					'name' => _x('Best Blog Items', 'post type general name'),
					'singular_name' => _x('Best Blog Item', 'post type singular name'),
					'add_new' => _x('Add New', 'Best Blog'),
					'add_new_item' => __('Add New Best Blog Item'),
					'edit_item' => __('Edit Best Blog Item'),
					'new_item' => __('New Best Blog Item'),
					'view_item' => __('View Best Blog Item'),
					'search_items' => __('Search Best Blog Items'),
					'not_found' =>  __('No Best Blog item found'),
					'not_found_in_trash' => __('No Best Blog items found in Trash'),
					'parent_item_colon' => '',
					'menu_name' => __('Best Blog Items'),
			),
			'singular_label' => __('Best Blog'),
			'public' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'show_ui' => true,
			'show_in_menu' => true,
			//'menu_position' => 20,
			'capability_type' => 'post',
			'hierarchical' => false,
			'supports'  =>  array(
					  'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail',
					  'custom-fields'
					),
			'taxonomies' => array('category'), // this is IMPORTANT
			'has_archive' => true,
			'rewrite' =>  array('slug'  =>  'best_of_blog', 'with_front' =>  false),
			'query_var' => false,
			'can_export' => true,
			'show_in_nav_menus' => false,
		));
	}
	
	public function video_post_type(){
		register_post_type('video', array(
			'labels' => array(
					'name' => _x('Video Items', 'post type general name'),
					'singular_name' => _x('Video Item', 'post type singular name'),
					'add_new' => _x('Add New', 'Video'),
					'add_new_item' => __('Add New Video Item'),
					'edit_item' => __('Edit Video Item'),
					'new_item' => __('New Video Item'),
					'view_item' => __('View Video Item'),
					'search_items' => __('Search Video Items'),
					'not_found' =>  __('No Video item found'),
					'not_found_in_trash' => __('No Video items found in Trash'),
					'parent_item_colon' => '',
					'menu_name' => __('Video Items'),
			),
			'singular_label' => __('Video'),
			'public' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'show_ui' => true,
			'show_in_menu' => true,
			//'menu_position' => 20,
			'capability_type' => 'post',
			'hierarchical' => false,
			'supports'  =>  array(
					  'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail',
					  'custom-fields'
					),
			'taxonomies' => array('category'), // this is IMPORTANT
			'has_archive' => true,
			'rewrite' =>  array('slug'  =>  'video', 'with_front' =>  false),
			'query_var' => false,
			'can_export' => true,
			'show_in_nav_menus' => false,
		));
	}
	
	public function humor_me_post_type(){
		register_post_type('humor_me', array(
			'labels' => array(
					'name' => _x('Humor me Items', 'post type general name'),
					'singular_name' => _x('Humor me Item', 'post type singular name'),
					'add_new' => _x('Add New', 'Humor me'),
					'add_new_item' => __('Add New Humor me Item'),
					'edit_item' => __('Edit Humor me Item'),
					'new_item' => __('New Humor me Item'),
					'view_item' => __('View Humor me Item'),
					'search_items' => __('Search Humor me Items'),
					'not_found' =>  __('No Humor me item found'),
					'not_found_in_trash' => __('No Humor me items found in Trash'),
					'parent_item_colon' => '',
					'menu_name' => __('Humor me Items'),
			),
			'singular_label' => __('Humor me'),
			'public' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'show_ui' => true,
			'show_in_menu' => true,
			//'menu_position' => 20,
			'capability_type' => 'post',
			'hierarchical' => false,
			'supports'  =>  array(
					  'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail',
					  'custom-fields','category'
					),
				   // 'taxonomies' => array('post_tag'), // this is IMPORTANT
			'has_archive' => true,
			'rewrite' =>  array('slug'  =>  'humor_me', 'with_front' =>  false),
			'query_var' => false,
			'can_export' => true,
			'show_in_nav_menus' => false,
		));
	}
	public function contest_post_type(){
		register_post_type('contest', array(
			'labels' => array(
					'name' => _x('Contest Items', 'post type general name'),
					'singular_name' => _x('Contest Item', 'post type singular name'),
					'add_new' => _x('Add New', 'Contest'),
					'add_new_item' => __('Add New Contest Item'),
					'edit_item' => __('Edit Contest Item'),
					'new_item' => __('New Contest Item'),
					'view_item' => __('View Contest Item'),
					'search_items' => __('Search Contest Items'),
					'not_found' =>  __('No Contest item found'),
					'not_found_in_trash' => __('No Contest items found in Trash'),
					'parent_item_colon' => '',
					'menu_name' => __('Contest Items'),
			),
			'singular_label' => __('Contest'),
			'public' => true,
			'publicly_queryable' => true,
			'exclude_from_search' => false,
			'show_ui' => true,
			'show_in_menu' => true,
			//'menu_position' => 20,
			'capability_type' => 'post',
			'hierarchical' => false,
			'supports'  =>  array(
					  'title', 'editor', 'comments', 'author', 'excerpt', 'thumbnail',
					  'custom-fields','category'
					),
				   // 'taxonomies' => array('post_tag'), // this is IMPORTANT
			'has_archive' => true,
			'rewrite' =>  array('slug'  =>  'contest', 'with_front' =>  false),
			'query_var' => false,
			'can_export' => true,
			'show_in_nav_menus' => false,
		));
	}
}
?>